package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.OrgDeviceInfoResponse
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import com.yanzhenjie.recyclerview.SwipeRecyclerView

/**
 * @Author      : hxw
 * @Date        : 2023/3/2 11:22
 * @Describe    :
 */
class DeviceInfoAdapter(data: ArrayList<OrgDeviceInfoResponse.ListBean>) :
    BaseQuickAdapter<OrgDeviceInfoResponse.ListBean, BaseViewHolder>(R.layout.item_device_info, data) {

    private var mDeleteListener: MyDeleteClickListener? = null
    private var mModifyListener: MyModifyClickListener? = null

    interface MyDeleteClickListener {
        fun clickDeleteListener(id: Int)
    }

    interface MyModifyClickListener {
        fun clickModifyListener(imsi: String, id: Int)
    }

    fun setOnDeleteClickListener(onItemDeleteClick: MyDeleteClickListener) {
        mDeleteListener = onItemDeleteClick
    }

    fun setOnModifyClickListener(onItemModifyClick: MyModifyClickListener) {
        mModifyListener = onItemModifyClick
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: BaseViewHolder, item: OrgDeviceInfoResponse.ListBean) {

        holder.setText(R.id.tv_item_member_info_name, context.getString(R.string.Participants_list) + ": " + item.EntryName)
        holder.setText(
            R.id.tv_item_member_info_shed_no,
            context.getString(R.string.activity_race_detail_pigeon_collection_member_no) + ": " + item.shedNo
        )
        val rvDeviceCode = holder.getView<SwipeRecyclerView>(R.id.rvDeviceCode)

        holder.itemView.apply {
            rvDeviceCode.adapter = null
            initSwipeGoodsItem(rvDeviceCode, item)
            if (data[holder.layoutPosition].info == null || data[holder.layoutPosition].info.isEmpty()) {
                return
            }
            val codeAdapter = CodeAdapter(item.info)
            rvDeviceCode.apply {
                layoutManager = LinearLayoutManager(this.context)
                adapter = codeAdapter
                codeAdapter.notifyDataSetChanged()
            }
        }
    }

    private fun initSwipeGoodsItem(rvDeviceCode: SwipeRecyclerView?, item: OrgDeviceInfoResponse.ListBean) {
        val swipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
            val width = context.resources.getDimensionPixelSize(R.dimen.dp_70)
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            val modifyItem = SwipeMenuItem(rvDeviceCode!!.context)
                .setBackground(R.drawable.round_b_g_5_train_fly_list_delete_blue)
                .setTextColor(Color.WHITE)
                .setText(context.getString(R.string.modify))
                .setTextSize(15)
                .setWidth(width)
                .setHeight(height)
            rightMenu.addMenuItem(modifyItem)

            val deleteItem = SwipeMenuItem(rvDeviceCode.context)
                .setBackground(R.drawable.round_b_g_5_train_fly_list_delete_red)
                .setTextColor(Color.WHITE)
                .setText(R.string.delete)
                .setTextSize(15)
                .setWidth(width)
                .setHeight(height)
            rightMenu.addMenuItem(deleteItem)
        }
        rvDeviceCode?.setSwipeMenuCreator(swipeMenuCreator)

        val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position ->
            /** 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱 */
            menuBridge.closeMenu()
            /**  menuBridge.position--菜单在Item中的Position */
            when (menuBridge.position) {
                0 -> {
                    /** 调用修改接口 */
                    if (mModifyListener != null) {
                        mModifyListener!!.clickModifyListener(item.info[position].imsi, item.info[position].id)
                    }
                }

                1 -> {
                    /** 调用删除接口 */
                    if (mDeleteListener != null) {
                        mDeleteListener!!.clickDeleteListener(item.info[position].id)
                    }
                }
            }
        }
        rvDeviceCode?.setOnItemMenuClickListener(mItemMenuClickListener)
    }
}